package com.dk.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dk.VO.TbUserVO;
import com.dk.constant.SeckillConstant;
import com.dk.mapper.TbSeckillCarMapper;
import com.dk.pojo.TbSeckillCar;
import com.dk.service.TbSeckillCarService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TbSeckillCarServiceImpl implements TbSeckillCarService {

    @Autowired
    private TbSeckillCarMapper seckillCarMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void prepareHot() {
        QueryWrapper<TbSeckillCar> wrapper = new QueryWrapper<>();

        Date date = new Date();

        wrapper.le("start_time",date);
        wrapper.ge("end_time",date);
        wrapper.eq("status","1");
        wrapper.gt("stock_count",0);

        List<TbSeckillCar> seckillCars = seckillCarMapper.selectList(wrapper);

        for (TbSeckillCar seckillCar : seckillCars) {
            redisTemplate.boundHashOps(SeckillConstant.SECKILL_LIST).put(seckillCar.getId(),seckillCar);
            stringRedisTemplate.boundValueOps(SeckillConstant.SECKILL_GOODS + seckillCar.getId()).set(String.valueOf(seckillCar.getStockCount()));
        }

    }

    @Override
    public List<TbSeckillCar> getSeckillList() {
        return redisTemplate.boundHashOps(SeckillConstant.SECKILL_LIST).values();
    }

    @Override
    public void createOrder(TbUserVO user, Long id) throws Exception {
        //根据商品id从redis获取数据
        TbSeckillCar seckillCar = getSeckillFromRedisById(id);
        //判断库存是否为空
        if (seckillCar.getStockCount() == 0){
            throw  new Exception("库存不足");
        }
        //将库存进行减一，并且返回扣减之后的值
        Long decrement = stringRedisTemplate.boundValueOps(SeckillConstant.SECKILL_GOODS + id).decrement();
        //判断扣减之后的库存是否为0，为0则更新数据库
        if (decrement == 0){
            TbSeckillCar seckill = getSeckillFromRedisById(id);
            seckill.setStockCount(0);
            seckillCarMapper.updateById(seckill);
        }
        //更新redis中库存的数据
        seckillCar.setStockCount(decrement.intValue());
        redisTemplate.boundHashOps(SeckillConstant.SECKILL_LIST).put(id,seckillCar);


        JSONObject jsonObject = new JSONObject();
        jsonObject.putOpt("id",id);
        jsonObject.putOpt("username",user.getUsername());
        rocketMQTemplate.convertAndSend(SeckillConstant.CREATE_ORDER_TOPIC,jsonObject.toString());
    }

    public TbSeckillCar getSeckillFromRedisById(Long id) {
        return (TbSeckillCar) redisTemplate.boundHashOps(SeckillConstant.SECKILL_LIST).get(id);
    }

    @Override
    public Long getOrderId(String username) {
        return (Long) redisTemplate.boundValueOps(SeckillConstant.SECKILL_ORDER + username).get();
    }
}
